package com.igelato.controller.AdminRole;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igelato.model.AdminRole;
import com.igelato.response.AppException;
import com.igelato.response.ErrorCodeMsgEnum;
import com.igelato.response.R;
import com.igelato.service.AdminRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;


/**
 * @Description: 角色
 * @Author: wht
 * @Date: 2023-12-30
 */
@Slf4j
@RestController
@RequestMapping("/")
@Api(tags = {"操作角色"})
public class AdminRoleController {
    @Autowired
    AdminRoleService adminRoleService;

    @ApiOperation("新增一个角色")
    @PutMapping("/addOneAdminRole")
    public <T> R<T> addOneAdminRole(@RequestBody AdminRoleAddDto addDto) {
        AdminRole adminRole = new AdminRole();
        BeanUtils.copyProperties(addDto, adminRole);
        boolean res = adminRoleService.save(adminRole);

        if (res) {
            return R.successMsg("添加成功！");
        } else {
            throw new AppException(ErrorCodeMsgEnum.ADD_ERROR);
        }
    }

    @ApiOperation("删除一个角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", dataType = "Integer", required = true)}
    )
    @DeleteMapping("/deleteOneAdminRole/{id}")
    public <T> R<T> deleteOneAdminRole(@PathVariable("id") Integer id) {
        if (id == null) {
            throw new AppException(ErrorCodeMsgEnum.REQUEST_PARAMS_ERROR);
        }
        boolean res = adminRoleService.removeById(id);

        if (res) {
            return R.successMsg("删除成功！");
        } else {
            throw new AppException(ErrorCodeMsgEnum.DELETE_ERROR);
        }
    }

    @ApiOperation("修改一个角色信息")
    @PatchMapping("/updateOneAdminRole")
    public <T> R<T> updateOneAdminRole(@RequestBody AdminRoleUpdateDto updateDto) {
        AdminRole adminRole = new AdminRole();
        BeanUtils.copyProperties(updateDto, adminRole);
        boolean res = adminRoleService.updateById(adminRole);

        if (res) {
            return R.successMsg("修改成功！");
        } else {
            throw new AppException(ErrorCodeMsgEnum.UPDATE_ERROR);
        }
    }

    @ApiOperation("通过id获取一个角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", dataType = "Integer", required = true)}
    )
    @GetMapping("/getOneAdminRole/{id}")
    public <T> R<T> getOneAdminRole(@PathVariable("id") Integer id) {
        if (id == null) {
            throw new AppException(ErrorCodeMsgEnum.REQUEST_PARAMS_ERROR);
        }
        AdminRole adminRole = adminRoleService.getById(id);
        return R.successData(adminRole);
    }

    @ApiOperation("分页获取角色列表")
    @PostMapping("/getAdminRoleList")
    public <T> R<T> getAdminRoleList(@Valid @RequestBody AdminRoleSearchDto searchDto) throws IllegalAccessException {
        QueryWrapper<AdminRole> queryWrapper = new QueryWrapper<>();

        Field[] fields = searchDto.getClass().getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            if (name.equals("page_num") || name.equals("page_size")) continue;
            field.setAccessible(true); // 必须将字段设置为可访问
            Object value = field.get(searchDto);
            if (value == null || (value instanceof String && "".equals(value))) continue;
                queryWrapper.eq(name, value);

        }

        Page<AdminRole> page = new Page<>(searchDto.getPage_num(), searchDto.getPage_size());
        adminRoleService.page(page, queryWrapper);

        HashMap<Object, Object> res = new HashMap<>();
        res.put("page_size", page.getSize());
        res.put("page_num", page.getCurrent());
        res.put("total", page.getTotal());
        res.put("list", page.getRecords());
        return R.successData(res);
    }

    @ApiOperation("获取所有角色列表")
    @GetMapping("/getAllAdminRoleList")
    public <T> R<T> getAllAdminRoleList() {
        List<AdminRole> list = adminRoleService.list();

        return R.successData(list);
    }
}
